

<?php


 require_once("../config.inc.php");
//require_once("../includes/db.class.php");
require_once("../classes/class.payment.php");
require_once('../includes/mailer.class.php');
require_once('../classes/class.mailshots.php');
require_once('../classes/user.class.php');

$paypal = new objPayment;
//$discount = new objDiscount;




$replyy = date("Y-m-d h:i:s")." -> "; //date for log file

// create vars for POST data
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$business = $_POST['business'];
$invoice = $_POST['invoice'];
$payment_status = $_POST['payment_status'];
$item_name = $_POST['item_name'];
$amount = $_POST['amount'];
$item_number = $_POST['item_number'];
$txn_id = $_POST['txn_id'];
$payment_gross = $_POST['payment_gross'];
$payment_date = $_POST['payment_date'];


/* PAP integration 
	 $ch = curl_init();
	 curl_setopt($ch, CURLOPT_URL, "http://partner.intelli4x.com/plugins/PayPal/paypal.php");
	 curl_setopt($ch, CURLOPT_POST, 1);
	 curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
	 curl_exec($ch);
 end of PAP integration 



$separator = '||';
    if ($_POST['custom'] != '') {
      $explodedCustomValue = explode($separator, $_POST['custom'], 2);
          if (count($explodedCustomValue) == 2) {
            $_REQUEST['custom'] = $_POST['custom'] = $explodedCustomValue[0];   
      }
    }
*/

//$subscr_effective = $_POST['subscr_effective'];
//$period1 = $_POST['period1'];
//$period2 = $_POST['period2'];
//$period3 = $_POST['period3'];
//$amount1 = $_POST['amount1'];
//$amount2 = $_POST['amount2'];
//$amount3 = $_POST['amount3'];
//$mc_amount1 = $_POST['mc_amount1'];
//$mc_amount2 = $_POST['mc_amount2'];
//$mc_amount3 = $_POST['mc_amount3'];
//$recurring = $_POST['recurring'];
//$reattempt = $_POST['reattempt'];
//$retry_at = $_POST['retry_at'];
//$recur_times = $_POST['recur_times'];
//$username = $_POST['username'];
//$password = $_POST['password'];
//$subscr_id = $_POST['subscr_id'];

////////////////////////////////////////////////////////////////
//// PAYPAL POST VERIFICATION /////
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

//form POST string
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
//$fp = fsockopen ('www.eliteweaver.co.uk', 80, $errno, $errstr, 30); // IPN testing URL - do not remove
//$fp = fsockopen ('http://www.belahost.com', 80, $errno, $errstr, 30); // IPN testing URL - do not remove



if (!$fp) {
	// HTTP ERROR
	$replyy .= "HTTP ERROR | $payer_email | $txn_type | $invoice";
} else {
	fputs ($fp, $header . $req);

	$ctr =1;


	while (!feof($fp)) {
		$res = fgets ($fp, 1024);



		if (strcmp ($res, "VERIFIED") == 0 ) { //!!! remove "|| $ctr == 1" and "$ctr++;" after testing
        //$ctr++;

			// check that receiver_email is  Primary PayPal email
			if ($paypal->businessID != $receiver_email && $paypal->businessID != $business) {
				exit  ;
			}

			$objSubscription = new objUserSubscription();
			$userInvoiceData = $objSubscription->getUserInvoiceData($invoice, $payment_status);
			//$discount->useDiscount($userInvoiceData['userID']);

			switch ($payment_status)   {
				case $objSubscription->paymentCompleted:
					$objSubscription->activateSubscription = 1;
					$objSubscription->subscriptionStart = time();
                   	$objSubscription->subscriptionEnd = true;

					/* PAP integration */
					 $ch = curl_init();
					 curl_setopt($ch, CURLOPT_URL, "http://partner.intelli4x.com/plugins/PayPal/paypal.php");
					 curl_setopt($ch, CURLOPT_POST, 1);
					 curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
					 curl_exec($ch);
					/* end of PAP integration */

					//mail 2 us about new subscriber
					$mailshot = new objMailshot();
					$mailshot->subscrNotify($payer_email, $objSubscription->userData['userID'], $objSubscription->userData['subscrTime'], $objSubscription->userData['subscrPeriod'], "subscr_new", "PayPal");

					//make discount inactive if user used it
	                //$discount->deactivateDiscount( $userInvoiceData['userID'] );
					break;
				default:
					break;
			}

			$objSubscription->processSubscription();


	        $response = "VERIFIED";
			//loop through the $_POST array and print all vars to the screen.
			/*
			foreach($_POST as $key => $value){
				echo $key." = ". $value."<br>";
			}
			*/

		}
		else if (strcmp ($res, "INVALID") == 0) {
			$response = "INVALID";
			echo "invalid";
	  	}

	}
	fclose ($fp);
}
//////////////////////////////////////////////////////////////




//////////////////////////////////////////////////////////////
//// LOG ALL EVENTS ////
$replyy .= "IPN $response | $payer_email | $txn_type | $invoice | $payment_status | $item_name";
$fd = fopen("../logs/ipn.log", "a");
fwrite($fd, $replyy."\r\n");
fclose($fd);



					
					echo "<br>".var_dump($_POST);

?>